package com.itheima.reggie.controller;

import cn.hutool.core.util.RandomUtil;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.SmsTemplate;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class UserController {

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private HttpSession session;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;
    //发送短信验证码
    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String, String> map) {
        //接收参数
        String phone = map.get("phone");
        //生成一个验证码 todo 临时写死验证，等到上线再开放
        //String code = RandomUtil.randomNumbers(6);
        String code = "123456";
        log.info("生成的验证码是：{}",code);

        //发送验证码 todo 等到生产环境再开放
        //smsTemplate.sendSms(phone,code);

        //记录下发送的验证码，方便后期对比
 //       session.setAttribute("USER_PHONE_CODE"+phone,code);

        //=======================调整1: 发送短信之后,需要将验证码存储到redis,而且要设置5分钟有效时间=========================//
        redisTemplate.opsForValue().set("USER_PHONE_CODE:"+phone,code,5, TimeUnit.MINUTES);

        //返回信息
        return ResultInfo.success(null);
    }

    //用户登录注册
    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String, String> map) {
        //接收参数
        String phone = map.get("phone");
        String codeFromFront = map.get("code");

        //比对验证码
//        String codeFromSession = (String) session.getAttribute("USER_PHONE_CODE" + phone);
//        if (StringUtils.isEmpty(codeFromSession) || !StringUtils.equals(codeFromFront, codeFromSession)) {
//            return ResultInfo.error("验证码错误");
//        }

        //=======================调整2: 以后在登录的时候,比对验证码就应该比对前台的和redis里面取出的=========================//
        String codeFromRedis = (String) redisTemplate.opsForValue().get("USER_PHONE_CODE:" + phone);
        if (StringUtils.isEmpty(codeFromRedis) || !StringUtils.equals(codeFromFront, codeFromRedis)) {
            return ResultInfo.error("验证码错误");
        }

        //调用service查询当前手机号在库中是否存在
        User user = userService.findByPhone(phone);
        if (user != null) {
            //查看状态是否处于禁用
            if (user.getStatus() == 0) {
                return ResultInfo.error("用户被禁用，请联系管理员");
            }
        } else {
            //没查到，代表新用户注册
            user = new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }

        //用户信息放入session
        session.setAttribute(Constant.SESSION_USER,user);

        //返回登录成功
        return ResultInfo.success("验证码发送成功");
    }

    @PostMapping("/user/logout")
    public ResultInfo logout() {
        session.invalidate();

        return ResultInfo.success(null);
    }

}
